package com.david.zhufengfm.Fragments.discover;


import android.content.Intent;
import android.os.Bundle;

import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import com.david.zhufengfm.AlbumDetailActivity;
import com.david.zhufengfm.Constants;
import com.david.zhufengfm.Fragments.BaseFragment;
import com.david.zhufengfm.R;
import com.david.zhufengfm.adapters.DiscoverRecommendAdapter;
import com.david.zhufengfm.adapters.PicPagerAdapter;
import com.david.zhufengfm.entity.discoverrecommend.AlbumRecommend;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendAlbums;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendFocus;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendItem;
import com.david.zhufengfm.entity.discoverrecommend.FocusRecommend;
import com.david.zhufengfm.tasks.DiscoverRecommendTask;
import com.david.zhufengfm.tasks.TaskCallback;
import com.david.zhufengfm.tasks.TaskResult;
import com.david.zhufengfm.utils.DimensionUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;


public class DiscoverRecommendFragment extends BaseFragment implements TaskCallback, View.OnClickListener {

    private DiscoverRecommendAdapter adapter;
    private List<DiscoverRecommendItem> items;
    private DiscoverRecommendFocus recommendFocus;
    private List<FocusRecommend>focusRecommends;
    /**
     * 轮播海报
     */
    private ViewPager focusImagesPager;
    private PicPagerAdapter picPagerAdapter;

    public DiscoverRecommendFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        items = new LinkedList<>();
        focusRecommends=new LinkedList<>();
        adapter=new DiscoverRecommendAdapter(getActivity(), items);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_discover_recommend, container, false);
        ListView listView = (ListView) ret.findViewById(R.id.discover_recommend_list);
        focusImagesPager=new ViewPager(getContext());
        AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                DimensionUtil.dp2px(getContext(),100));
        focusImagesPager.setLayoutParams(lp);
        //假数据
//        LinkedList picData=new LinkedList();
//        for (int i = 0; i < 5; i++) {
//            picData.add(1);
//        }

        picPagerAdapter = new PicPagerAdapter(focusRecommends);
        focusImagesPager.setAdapter(picPagerAdapter);
        listView.addHeaderView(focusImagesPager);

        //设置专辑推荐点击事件
        adapter.setOnRecommendAlbumClickListener(this);
        listView.setAdapter(adapter);
        focusImagesPager.setCurrentItem(Integer.MAX_VALUE>>1);
        return ret;
    }

    /**
     * 因为每次进入推荐的时候，都会触发内容的刷新
     * 因此网络加载，onResume中
     */
    @Override
    public void onResume() {
        super.onResume();
        DiscoverRecommendTask task=new DiscoverRecommendTask(this);
        task.execute();
    }

    @Override
    public String getFragmentTitle() {
        return "推荐";
    }

    @Override
    public void onTaskFinished(TaskResult result) {

        if (result != null) {
            //TODO 处理推荐列表数据
            if (result.action== Constants.TASK_ACTION_DISCOVER_RECOMMEND){
                if (result.resultCode==Constants.TASK_RESULT_OK){
                    Object data=result.data;
                    if (data != null)
                        if (data instanceof List) {
                            List list = (List) data;
                            //TODO 更新ListView Adapter
                            //只要数据来了,就清楚adapter
                            items.clear();
                            for (Object o : list) {
                                if (o instanceof DiscoverRecommendItem) {
                                    items.add((DiscoverRecommendItem) o);
                                }
                            }
                            int itemsLength=items.size();
                            recommendFocus= (DiscoverRecommendFocus) items.get(itemsLength-1);
                            items.remove(itemsLength-1);
                            focusRecommends.addAll(recommendFocus.getFocusList());
                            adapter.notifyDataSetChanged();
                            picPagerAdapter.notifyDataSetChanged();

                        }else if(data instanceof JSONObject){
                            //1.解析轮播海报
                            JSONObject jsonObject= (JSONObject) data;
                            try {
                                JSONObject focusImages = jsonObject.getJSONObject("focusImages");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                }else{
                    //TODO 提示错误信息
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        Object tag=v.getTag();

        if (tag != null) {
            if (tag instanceof AlbumRecommend){

                AlbumRecommend albumRecommend= (AlbumRecommend) tag;
                //专辑ID
                long albumId = albumRecommend.getAlbumId();
                //曲目ID
                long trackId = albumRecommend.getTrackId();
                Intent intent=new Intent(getActivity(), AlbumDetailActivity.class);
                intent.putExtra("albumId", albumId);
                intent.putExtra("trackId",trackId);
                Log.i("albumId",""+albumId);
                getActivity().startActivity(intent);
            }
        }
    }
}
